# encoding: utf-8
# Author    : limusen
# Datetime  : 2023/9/27 09:54
# File      : browse.py
# explain   : 文件说明

import os
import warnings
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import platform
from common.config import local_config
from common.logs import logger


class Browser:
    def __init__(self, path=None):
        warnings.filterwarnings("ignore", category=DeprecationWarning)
        self.driver_path = path or os.path.join(
            os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir)), local_config.driver_path)
        self.driver_name = local_config.default_driver.lower()

    def get_driver(self):
        logger.info(f"当前正在打开：{self.driver_name}")

        driver_map = {
            "chrome": self.__get_chrome_driver,
            "firefox": self.__get_firefox_driver,
            "edge": self.__get_edge_driver
        }

        if self.driver_name in driver_map:
            return driver_map[self.driver_name]()
        else:
            logger.error("Unsupported browser specified.")
            return None

    def __get_chrome_driver(self):
        chrome_options = self.__get_common_chrome_options()
        system = platform.system().lower()

        if system == "darwin":
            chrome_path = os.path.join(self.driver_path, 'chromedriver')
        elif system == "win32":
            chrome_path = os.path.join(self.driver_path, 'chromedriver.exe')
        elif system == "linux":
            chrome_options.add_argument('no-sandbox')
            chrome_options.add_argument("headless")
            chrome_options.add_argument('disable-dev-shm-usage')
            chrome_options.add_argument('disable-gpu')
            chrome_path = None
        else:
            logger.error("Unsupported operating system.")
            return None

        driver = webdriver.Chrome(executable_path=chrome_path, options=chrome_options)
        return driver

    def __get_common_chrome_options(self):
        chrome_options = Options()
        chrome_options.add_argument('--disable-gpu')
        chrome_options.add_argument('lang=zh_CN.UTF-8')
        chrome_options.add_experimental_option('useAutomationExtension', False)
        chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])
        return chrome_options

    def __get_firefox_driver(self):
        system = platform.system().lower()
        firefox_driver_path = os.path.join(self.driver_path, 'geckodriver' if system == "darwin" else 'geckodriver.exe')
        driver = webdriver.Firefox(executable_path=firefox_driver_path)
        logger.info('初始化Firefox浏览器并启动')
        return driver

    def __get_edge_driver(self):
        edge_driver_path = os.path.join(self.driver_path, 'msedgedriver.exe')
        driver = webdriver.Edge(executable_path=edge_driver_path)
        logger.info('初始化Edge浏览器并启动')
        return driver


if __name__ == "__main__":
    drivers = Browser().get_driver()
    drivers.get('https://www.baidu.com')
    drivers.quit()
